home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / ENCRYPT.SWG / 0020_Store Hidden Text.pas < prev   
Pascal/Delphi Source File  |  1994-08-24  |  10KB  |  325 lines

  1. {
  2.             HIDESTR -- Rich Veraa's Anti-hack string hider.
  3.  
  4.                     Released to the Public Domaim
  5.                           22 April, 1992
  6.                         by Richard P. Veraa
  7.  
  8.  
  9. INTRODUCTION
  10.  
  11.      The purpose of HIDESTR is to encrypt string variables in a Turbo
  12.      Pascal <tm> program so that they will be hidden in the .EXE file
  13.      and will make things a bit more difficult for hackers.
  14.  
  15.      HIDESTR reads an ASCII list of text strings used in the program and
  16.      creates Turbo Pascal <tm> source code for a TPU unit that includes
  17.      the strings encrypted as constant arrays of bytes. There is a
  18.      decrypting procedure to be used at run time to return the decrypted
  19.      strings as functions.
  20.  
  21.      To use HIDESTR, just
  22.  
  23.      1. list the text strings to be used by the program in a text file
  24.      named LIST with numbered lines, as follows:
  25.  
  26. 1   Myprog
  27. 2   Version 1.0
  28. 3   by John Doe
  29. 4   Enter velocity, mph:
  30. 5   Enter time of trip, hours:
  31. 6   The distance traveled is
  32. 7   miles.
  33. 8   Do you wish to go on [Y/n]?
  34. 9   Done
  35. 10  Thank you for using MYPROG
  36.  
  37.      2. Then write your program using str1, str2, str3, etc in place of
  38.      the strings.
  39.  
  40.      4. Place the TPU name "STRLIST" in your "uses" statement. The
  41.      following is code for a typical small program:
  42.  
  43. Program Myprog;
  44. uses strlist;
  45.  
  46. var
  47.    v, t, d : real;
  48.    ch : char;
  49.  
  50. begin
  51.    ch := 'y';
  52.    writeln (srt1,' ',srt2);
  53.    writeln(str3);
  54.    writeln;
  55.    while not ch in ['n','N'] do
  56.       begin
  57.          write(str4,' ');
  58.          readln(v);
  59.          write(str5,' ');
  60.          readln(t);
  61.          writeln;
  62.          d := v * t;
  63.          writeln(str6,' ',d);
  64.       end;
  65.    writeln;
  66.    write(str8,' ');
  67.    readln(ch);
  68.    writeln;
  69.    writeln(str9);
  70.    writeln(str10);
  71. end.
  72.  
  73.  
  74.      5. Run HIDESTR in the same directory as the file LIST, with any
  75.      valid longint on the command line. The longint is the key for
  76.      encrypting the strings, and functions as randseed for the Turbo
  77.      random number generator, whose output is added to the strings to
  78.      encode them byte by byte;
  79.  
  80.      HIDESTR will generate TP source code for the STRLIST.TPU, which may
  81.      be compiled with the program. The strings will appear in the
  82.      resulting EXE file as arrays of random-appearing bytes.
  83.  
  84.      The encryption technique is admittedly crude, and you may wish to
  85.      improve on it, but It would take a very determined hacker to take
  86.      the trouble to unscramble this.
  87.  
  88. program hidestr;  {v 1.2}
  89. {       By Richard Veraa                                        }
  90. {       Villa Maria, Room 211                                   }
  91. {       1050 NE 125 Street                                      }
  92. {       N. Miami, FL  33161                                     }
  93. {          released into the public domain, April 23, 1992      }
  94. uses crt;
  95. const
  96.    key : longint = 1111111;       {default encryption key}
  97.                                    {change to any number}
  98. type
  99.    stringptr = ^string;
  100.    byteptr = ^byte;
  101.    bytearray = array[1..255] of byte;
  102.  
  103. var
  104.    str : array[1..255] of stringptr;
  105.    l : array[1..255] of byteptr;
  106.    th : array[1..255] of boolean;
  107.    n : integer;
  108.    ba : bytearray;
  109.    i, j : integer;
  110.    f2 : text;
  111.    spacecount : integer;
  112.    x, y : byte;
  113.    keystring : string;
  114.    code : integer;
  115.  
  116.    procedure crypt(var b : bytearray; l : byte);
  117.     {Add random number to each byte}
  118.       var
  119.          i : integer;
  120.          r : byte;
  121.          save : byte;
  122.       begin
  123.          randseed := key;
  124.          for i := 1 to l do
  125.             begin
  126.                r := random(255);
  127.                b[i] := b[i] + r;
  128.             end;
  129.       end;
  130.  
  131.    procedure decrypt(var b : bytearray; l : byte);
  132.     {Subtract number from each byte}
  133.       var
  134.          i : integer;
  135.          r : byte;
  136.       begin
  137.          randseed := key;
  138.          for i := 1 to l do
  139.             begin
  140.                r := random(255);
  141.                b[i] := b[i] - r
  142.             end;
  143.       end;
  144.  
  145.    procedure readfile;
  146.       var
  147.          f : text;
  148.          s : string;
  149.          len, i : integer;
  150.       begin
  151.          n := 0;
  152.          assign(f,'list');
  153.          reset(f);
  154.          while not eof(f) do
  155.             begin
  156.                inc(n);
  157.                read(f,i);   {read line number}
  158.                if i <> n then
  159.                   begin
  160.                      Writeln('Error in LIST.');
  161.                      Writeln('  -- Numbering incorrect at line ',n);
  162.                      Writeln;
  163.                      Halt(n);
  164.                   end;
  165.                readln(f,s);      {read string}
  166.                while s[1] = chr( $20) do  {remove leading blanks}
  167.                   begin
  168.                        len := length(s);
  169.                        dec(len);
  170.                        for i := 1 to len do
  171.                           s[i] := s[i+1];
  172.                        s[0] := chr(len);
  173.                   end;
  174.                str[n]^ := s;
  175.                l[n]^ := length(str[n]^)
  176.             end;
  177.          close(f);
  178.       end;
  179.  
  180.  
  181. var
  182.    s : string;
  183.  
  184. begin
  185.    clrscr;
  186.    writeln('Rich Veraa''s little string hider unit maker');
  187.    writeln('Version 1.2');
  188.  
  189.    writeln;
  190.    if paramcount > 0 then     {check for key on command line}
  191.       begin
  192.          keystring := paramstr(1);
  193.          val(keystring,key,code);
  194.          if code <> 0 then
  195.             begin
  196.                writeln('Parameter should be key in form longint');
  197.                writeln(' * * * Parameter error ',code,' * * * ');
  198.                writeln;
  199.                halt(code);
  200.             end;
  201.       end;
  202.    x := wherex;
  203.    y := wherey;
  204.    for i := 1 to 255 do   {allocate memory}
  205.       begin
  206.          new(str[i]);
  207.          new(l[i]);
  208.       end;
  209.    for i := 1 to 255 do    {initialize}
  210.       begin
  211.          th[i] := false;
  212.          l[i]^ := 0;
  213.          str[i]^ :='';
  214.       end;
  215.    readfile;              {read LIST}
  216.    for i := 1 to 255 do   {set lengths for arrays to hold strings}
  217.       begin
  218.          for j := 1 to 255 do
  219.             if l[i]^ = j then th[j] := true;
  220.       end;
  221.    assign(f2,'strlist.pas');    {write source code for strlist.pas}
  222.    rewrite(f2);
  223.    writeln(f2,'unit strlist;');
  224.  
  225.    writeln(f2,'interface');
  226.  
  227.    writeln(f2,'type');           {type statement}
  228.    writeln(f2,'   bytearray = array[1..255] of byte;');
  229.    for i := 1 to 255 do          {type arrays for encrypted strings}
  230.    if th[i] then  writeln(f2,'   t',i,' = string[',i,'];');
  231.  
  232.    writeln(f2,'const');
  233.    writeln(f2,'     n = ',n,';');
  234.    writeln(f2,'   key = ',key,';');
  235.    for i := 1 to n do if l[i]^ > 1 then
  236.       begin
  237.         for j := 1 to l[i]^ do      {place string in array of byte}
  238.            ba[j] := ord(str[i]^[j]);
  239.          crypt(ba,l[i]^);           {encrypt bytes in array}
  240.          gotoxy(x,y);
  241.          write('Encrypted ',i,' strings.');
  242.          spacecount := 34;
  243.          write(f2,'   cr',i,' : array[1..',l[i]^,'] of byte = (');
  244.          for j := 1 to l[i]^ do     {list array as constant in strlist.pas}
  245.             begin
  246.                write(f2,ba[j]);
  247.                inc(spacecount,2);
  248.                if ba[j] > 9 then inc(spacecount);
  249.                if ba[j] > 99 then inc(spacecount);
  250.                if (spacecount > 72) and (j < l[i]^) then
  251.                   begin
  252.                      writeln(f2,',');
  253.                      write(f2,'        ');
  254.                      spacecount := 10;
  255.                   end
  256.                      else if j < l[i]^ then write(f2,',');
  257.             end;  {for j}
  258.          writeln(f2,');');
  259.       end;  {for i}
  260.    writeln;
  261.    writeln;
  262.    x := wherex;
  263.    y := wherey;
  264.  
  265.    writeln(f2,'   procedure decrypt(var b : bytearray; l : byte);');
  266.    for i := 1 to n do if l[i]^ > 1 then
  267.       begin
  268.          writeln(f2,'   function str',i,' : t',l[i]^,';');
  269.       end;
  270.  
  271.    writeln(f2,'implementation');
  272.                  {write source code for decrypt procedure}
  273.  
  274.    writeln(f2,'   procedure decrypt(var b : bytearray; l : byte);');
  275.    writeln(f2,'   var');
  276.    writeln(f2,'      i : integer;');
  277.    writeln(f2,'      r : byte;');
  278.    writeln(f2,'   begin');
  279.    writeln(f2,'      randseed := key;');
  280.    writeln(f2,'      for i := 1 to l do');
  281.    writeln(f2,'         begin');
  282.    writeln(f2,'            r := random(255);');
  283.    writeln(f2,'            b[i] := b[i] - r;');
  284.  
  285.    writeln(f2,'         end;');
  286.    writeln(f2,'   end;');
  287.  
  288.    for i := 1 to n do if l[i]^ > 1 then
  289.       begin
  290.           {write source code for function to return string}
  291.          writeln(f2,'   function str',i,' : t',l[i]^,';');
  292.          writeln(f2,'      var');
  293.          writeln(f2,'         ba : bytearray;');
  294.          writeln(f2,'         j : integer;');
  295.          writeln(f2,'         s : string;');
  296.          writeln(f2,'      begin');
  297.          writeln(f2,'        for j := 1 to ',l[i]^,' do');
  298.          writeln(f2,'          ba[j] := cr',i,'[j];');
  299.          writeln(f2,'        decrypt(ba, ',l[i]^,');');
  300.          writeln(f2,'        for j := 1 to ',l[i]^,' do');
  301.          writeln(f2,'          s[j] := chr(ba[j]);');
  302.          writeln(f2,'        s[0] := chr(',l[i]^,');');
  303.          writeln(f2,'        str',i,' := s;');
  304.          writeln(f2,'      end;');
  305.          gotoxy(x,y);
  306.          write('String functions coded: ',i);
  307.  
  308.       end;
  309.    gotoxy(x,y);
  310.    writeln;
  311.    writeln;
  312.  
  313.    writeln(f2,'begin');
  314.  
  315.    writeln(f2,'end.');
  316.    close(f2);
  317.    writeln('DONE');
  318.    for i := 1 to 255 do      {dispose}
  319.       begin
  320.          dispose(str[i]);
  321.          dispose(l[i]);
  322.       end;
  323.    writeln;
  324. end.
  325.